package com.epro.dx.util;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Created by liangzili on 15/8/3.
 */
public class SystemUtils {
	/**
	 * 判断应用是否已经启动
	 * 
	 * @param context
	 *            一个context
	 * @param packageName
	 *            要判断应用的包名
	 * @return boolean
	 */
	public static boolean isAppAlive(Context context, String packageName) {
		ActivityManager activityManager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<ActivityManager.RunningAppProcessInfo> processInfos = activityManager
				.getRunningAppProcesses();
		for (int i = 0; i < processInfos.size(); i++) {
			if (processInfos.get(i).processName.equals(packageName)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 判断应用BaseActivity是否存在
	 * @param context
	 * @return
	 */
	public static boolean isBaseActivityRunning(Context context) {
		ActivityManager am = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> tasks = am.getRunningTasks(1);
		if (tasks != null && !tasks.isEmpty()) {
			for (int i = 0; i < tasks.size(); i++) {
				Log.i("ffff",tasks.get(i).baseActivity.getPackageName());
				
			}
			ComponentName topActivity = tasks.get(0).baseActivity;
			if (topActivity.getClassName().equals("com.epro.dx.activity.HomeActivity")) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 判断activity是否运行
	 * @param context
	 * @param activityName //com.epro.dx.activity.HomeActivity
	 * @return
	 */
	public static boolean isActivityRunning(Context context,String activityName) {
		Intent intent = new Intent();
		intent.setClassName("com.epro.dx", "com.epro.dx.activity.HomeActivity");
		if (context.getPackageManager().resolveActivity(intent, 0) == null) {
			return false;
		}
		Log.i("ffff","isActivityRunning "+true);
		return true;
	}

}
